﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace Upreader.Common
{
    public static class ReflectionUtility
    {
        public static string GetPropertyName<T>(Expression<Func<T, object>> expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            MemberExpression body = expression.Body as MemberExpression;
            if (body == null) 
            {
                UnaryExpression unaryExpression = expression.Body as UnaryExpression;

                if (unaryExpression != null && unaryExpression.NodeType == ExpressionType.Convert)
                    body = unaryExpression.Operand as MemberExpression; 
            }

            return body.Member.Name;
        }

        public static IEnumerable<string> GetPropertyNames<T>(BindingFlags bindingFlags)
        {
            Type type = typeof(T);

            return type.GetProperties(bindingFlags)
                       .Select(x => x.Name);
        }
    } 
}
